﻿function UploadFileTypeValidatorEvaluateIsValid(val)
{
	// 获取控件值
	var controlValue = UploadFileTypeValidatorGetValue(val.controltovalidate);
	if (typeof(controlValue) != "string" || controlValue == "")
		return true;
		
	// 获取文件类型
	var uploadType = GetFileType(controlValue);
	if (typeof(uploadType) != "string" || uploadType == "")
		return false;
	
	//
	allowUploadType = val.allowUploadType;
	
	var isAllow = false;

	if (!(typeof(allowUploadType) != "string" || allowUploadType == ""))
	{
		var re = new RegExp("^(" + allowUploadType + ")$", "gi");
		if(uploadType.match(re))
			isAllow = true;
	}

	return isAllow;
}

// 获取文件类型
function GetFileType(fileName)
{
	if (typeof(fileName) != "string" || fileName == "")
		return null;

	var position = fileName.lastIndexOf(".");
	if (position == -1)
		return null;
	return fileName.substring(position + 1);
}

// Get From System
function UploadFileTypeValidatorGetValue(id) {
    var control;
    control = document.getElementById(id);
    if (typeof(control.value) == "string") {
        return control.value;
    }
    return UploadFileTypeValidatorGetValueRecursive(control);
}

function UploadFileTypeValidatorGetValueRecursive(control)
{
    if (typeof(control.value) == "string" && (control.type != "radio" || control.checked == true)) {
        return control.value;
    }
    var i, val;
    for (i = 0; i<control.childNodes.length; i++) {
        val = UploadFileTypeValidatorGetValueRecursive(control.childNodes[i]);
        if (val != "") return val;
    }
    return "";
}

function UploadFileTypeValidatorTrim(s) {
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}